<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-row>
        <el-col :span="4.5">
          <el-form-item label="用户姓名" prop="userName">
            <el-input v-model="queryParams.userName" placeholder="请输入用户姓名" clearable @keyup.enter.native="handleQuery"
              size="mini" />
          </el-form-item>
        </el-col>
        <el-col :span="4.5">
          <el-form-item label="用户手机号" prop="phoneNumber">
            <el-input v-model="queryParams.phoneNumber" placeholder="请输入用户手机号" clearable
              @keyup.enter.native="handleQuery" size="mini" />
          </el-form-item>
        </el-col>
        <el-col :span="4.5">
          <el-form-item label="门店名称" prop="storeName">
            <el-input v-model="queryParams.storeName" placeholder="请输入门店名称" clearable @keyup.enter.native="handleQuery"
              size="mini" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="4.5">
          <el-form-item label="订单类型" prop="receiveAddress">
            <el-select v-model="queryParams.orderType" clearable placeholder="请选择" size="mini">
              <el-option v-for="item in orderTypeOpetions" :key="item.id" :label="item.name"
                :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col> -->
        <!-- <el-col :span="4.5">
          <el-form-item label="订单类型" prop="receiveAddress" size="mini">
            <el-select v-model="queryParams.orderType" clearable placeholder="请选择">
              <el-option v-for="item in orderTypeOpetions" :key="item.id" :label="item.name"
                :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col> -->
        <el-col :span="4">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-form-item label="区域id" prop="regionId">
        <el-input
          v-model="queryParams.regionId"
          placeholder="请输入区域id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="区域名称" prop="regionName">
        <el-input
          v-model="queryParams.regionName"
          placeholder="请输入区域名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="门店id" prop="storeId">
        <el-input
          v-model="queryParams.storeId"
          placeholder="请输入门店id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="支付时间" prop="paymentTime">
        <el-date-picker clearable
          v-model="queryParams.paymentTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择支付时间">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="收货人姓名" prop="receiveName">
        <el-input
          v-model="queryParams.receiveName"
          placeholder="请输入收货人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="收货人电话" prop="receivePhonenum">
        <el-input
          v-model="queryParams.receivePhonenum"
          placeholder="请输入收货人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="收货地址经度" prop="receiveLongitude">
        <el-input
          v-model="queryParams.receiveLongitude"
          placeholder="请输入收货地址经度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货地址纬度" prop="receiveLatitude">
        <el-input
          v-model="queryParams.receiveLatitude"
          placeholder="请输入收货地址纬度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <!-- <el-form-item label="发货时间" prop="sendTime">
        <el-date-picker clearable
          v-model="queryParams.sendTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发货时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="收货时间" prop="receiveTime">
        <el-date-picker clearable
          v-model="queryParams.receiveTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择收货时间">
        </el-date-picker>
      </el-form-item> -->
    </el-form>
    <!-- <span>订单总额：1000.01</span> -->
    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange" height="730px">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="id" width="170" />
      <el-table-column label="商家信息" align="center" prop="storeName" :show-overflow-tooltip='true' width="220">
        <template slot-scope="{row}">
          <div class="table-user">
            <div class="table-user-img">
              <image-preview :src="row.storeImg || '@/assets/images/默认头像.jpg'" class="img" />
            </div>
            <div>
              <div class="table-user-name">名称：{{ row.receiveName }}</div>
              <div class="table-user-phone">电话号：{{ row.receivePhonenum }}</div>
            </div>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="用户手机号" align="center" prop="phoneNumber" /> -->
      <!-- <el-table-column label="区域名称" align="center" prop="regionName" /> -->

      <el-table-column label="实付金额" align="center" prop="payMoney" />
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.orderStatus == 1" style="color: chocolate;">待发货</span>
          <span v-if="scope.row.orderStatus == 2" style="color: cornflowerblue;">配送中</span>
          <span v-if="scope.row.orderStatus == 3" style="color: lightblue;">待评价</span>
          <span v-if="scope.row.orderStatus == 4" style="color: green;">已完成</span>
          <span v-if="scope.row.orderStatus == 5" style="color: red;">已退单</span>
        </template>
      </el-table-column>
      <el-table-column label="供应商信息" align="center" prop="userName" width="220">
        <template slot-scope="{row}">
          <div class="table-user">
            <div class="table-user-img">
              <image-preview :src="row.supplierImg || '@/assets/images/默认头像.jpg'" class="img" />
            </div>
            <div>
              <div class="table-user-name">名称：{{ row.supplierName }}</div>
              <div class="table-user-phone">电话号：{{ row.supplierPhone }}</div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="支付时间" align="center" prop="paymentTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paymentTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column label="商户订单号" align="center" prop="outTradeNo" />
      <el-table-column label="微信支付订单号" align="center" prop="transactionId" /> -->
      <el-table-column label="收货人信息" align="center" prop="receiveName" width="270">
        <template slot-scope="{row}">
          <div class="table-user">
            <!-- <div class="table-user-img">
              <img :src="row.storeImg">
            </div> -->
            <div>
              <div class="table-user-name">姓名：{{ row.receiveName }}</div>
              <div class="table-user-phone">电话号：{{ row.receivePhonenum }}</div>
              <div class="table-user-phone">收货地址：{{ row.receiveAddress }}</div>
            </div>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="收货地址" align="center" prop="receiveAddress" :show-overflow-tooltip='true' />
      <el-table-column label="收货人电话" align="center" prop="receivePhonenum" /> -->
      <!-- <el-table-column label="收货地址经度" align="center" prop="receiveLongitude" />
      <el-table-column label="收货地址纬度" align="center" prop="receiveLatitude" /> -->
      <el-table-column label="发货时间" align="center" prop="sendTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货时间" align="center" prop="receiveTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.receiveTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="订单类型" align="center" prop="orderType">
        <template slot-scope="scope">
          <span v-if="scope.row.orderType == 1">采购订单</span>
          <span v-if="scope.row.orderType == 2">普通订单</span>
        </template>
      </el-table-column> -->
      <el-table-column label="配送方式" align="center" prop="deliveryType">
        <template slot-scope="scope">
          <span v-if="scope.row.deliveryType == 1">普通</span>
          <span v-if="scope.row.deliveryType == 2">加急</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="配送费" align="center" prop="deliveryCost" />
      <el-table-column label="分销金额" align="center" prop="distributionValue" /> -->
      <!-- <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip='true' /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="togetItemList(scope.row)">详情</el-button>
          <el-button size="mini" type="text" v-if="scope.row.orderStatus == 1 && isDisplayed"
            @click="delivery(scope.row)" v-hasPermi="['system:purchase:order']">接单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 设置配送人 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="发货方式" prop="receiveTime">
          <el-select v-model="value" placeholder="请选择发货方式">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户姓名" />
        </el-form-item>
        <el-form-item label="用户手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入用户手机号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="DetailOpen" width="1100px" append-to-body>
      <el-table v-loading="loading" :data="itemList">
        <el-table-column label="订单号" align="center" prop="orderId" />
        <el-table-column label="商品名称" align="center" prop="goodName" />
        <el-table-column label="图片" align="center" prop="goodImg" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.goodImg" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="分类" align="center" prop="goodTypeName" />
        <el-table-column label="商品编号" align="center" prop="supplierGoodId" />
        <el-table-column label="商品价格" align="center" prop="goodPrice" />
        <el-table-column label="采购数量" align="center" prop="goodCount" />
        <el-table-column label="下单时间" align="center" prop="createTime" width="180">
          <!-- <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
            </template> -->
        </el-table-column>
      </el-table>
    </el-dialog>
    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户姓名" />
        </el-form-item>
        <el-form-item label="用户手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入用户手机号" />
        </el-form-item>
        <el-form-item label="区域id" prop="regionId">
          <el-input v-model="form.regionId" placeholder="请输入区域id" />
        </el-form-item>
        <el-form-item label="区域名称" prop="regionName">
          <el-input v-model="form.regionName" placeholder="请输入区域名称" />
        </el-form-item>
        <el-form-item label="门店id" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入门店id" />
        </el-form-item>
        <el-form-item label="门店名称" prop="storeName">
          <el-input v-model="form.storeName" placeholder="请输入门店名称" />
        </el-form-item>
        <el-form-item label="实付金额" prop="payMoney">
          <el-input v-model="form.payMoney" placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="支付时间" prop="paymentTime">
          <el-date-picker clearable v-model="form.paymentTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="配送费" prop="deliveryCost">
          <el-input v-model="form.deliveryCost" placeholder="请输入配送费" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="收货人姓名" prop="receiveName">
          <el-input v-model="form.receiveName" placeholder="请输入收货人姓名" />
        </el-form-item>
        <el-form-item label="收货地址" prop="receiveAddress">
          <el-input v-model="form.receiveAddress" placeholder="请输入收货地址" />
        </el-form-item>
        <el-form-item label="收货人电话" prop="receivePhonenum">
          <el-input v-model="form.receivePhonenum" placeholder="请输入收货人电话" />
        </el-form-item>
        <el-form-item label="收货地址经度" prop="receiveLongitude">
          <el-input v-model="form.receiveLongitude" placeholder="请输入收货地址经度" />
        </el-form-item>
        <el-form-item label="收货地址纬度" prop="receiveLatitude">
          <el-input v-model="form.receiveLatitude" placeholder="请输入收货地址纬度" />
        </el-form-item>
        <el-form-item label="发货时间" prop="sendTime">
          <el-date-picker clearable v-model="form.sendTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择发货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收货时间" prop="receiveTime">
          <el-date-picker clearable v-model="form.receiveTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择收货时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <PrinteOrder ref="printer-order" :orderLists="orderLists" class="invisible-element" />
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/fx/order";
import { listItem } from "@/api/fx/orderitem";
import Cookies from "js-cookie";
import PrinteOrder from '@/components/Printer/index.vue'


export default {
  name: "Order",
  components: { PrinteOrder },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      DetailOpen: false,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      itemList: [],
      itemQueryParams: {
        pageNum: 1,
        pageSize: 1000,
        orderId: 0,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        phoneNumber: null,
        regionId: null,
        regionName: null,
        storeId: null,
        storeName: null,
        paymentTime: null,
        orderStatus: null,
        receiveName: null,
        receiveAddress: null,
        receivePhonenum: null,
        receiveLongitude: null,
        receiveLatitude: null,
        sendTime: null,
        receiveTime: null,
        orderType: null,
        deliveryType: null,
        orderType: 4
      },
      // 表单参数
      form: {},
      orderTypeOpetions: [{ id: "1", name: "采购订单" }, { id: "2", name: "普通订单" }],
      // 表单校验
      rules: {
        receiveName: [
          { required: true, message: "收货人姓名不能为空", trigger: "blur" }
        ],
      },
      isDisplayed: false,
      orderLists: {}
    };
  },
  created() {
    const roles = JSON.parse(Cookies.get("roles"));
    console.log(roles, 8888);

    roles.forEach(item => {
      if (item !== 'admin' && item !== 'platform') {
        this.isDisplayed = true
      }
    })
    this.getList();
  },

  methods: {

    /**
     * 发货
     *
     */
    delivery(data) {
      const params = {
        id: data.id,
        orderStatus: 2
      }
      this.orderLists = data
      this.orderLists.userName = data.receiveName

      updateOrder(params).then(res => {
        if (res.code == 200) {
          this.$message.success('接单成功！')
          this.getList()
        } else {
          this.$message.error('接单失败！')
        }
      })
    },

    togetItemList(row) {
      this.DetailOpen = true;
      this.getItemList(row);
    }
    ,
    getItemList(row) {
      this.loading = true;
      this.itemQueryParams.orderId = row.id;
      listItem(this.itemQueryParams).then(response => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },


    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        userName: null,
        phoneNumber: null,
        regionId: null,
        regionName: null,
        storeId: null,
        storeName: null,
        payMoney: null,
        paymentTime: null,
        deliveryCost: null,
        orderStatus: null,
        createTime: null,
        updateTime: null,
        remark: null,
        isDelete: null,
        outTradeNo: null,
        transactionId: null,
        outRefundNo: null,
        receiveName: null,
        receiveAddress: null,
        receivePhonenum: null,
        receiveLongitude: null,
        receiveLatitude: null,
        sendTime: null,
        receiveTime: null,
        orderType: null,
        deliveryType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function () {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>


<style lange="sass" scoped>
.table-user {
  width: 100%;
  height: 100%;
  display: flex;
  /* justify-content: center; */
  align-items: center;
  text-align: left !important;

  .table-user-img {
    width: 2vw;
    height: 4vh;
    margin-right: 10px;

    .img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      object-fit: cover;
      overflow: hidden;

    }
  }

}
</style>
